home
diamond Go Premium
Data Engineering Path  ·  PySpark

JSON Ingestion

JSON (JavaScript Object Notation) is a highly flexible, semi-structured data format widely used for APIs, event streaming, and nested datasets. Unlike CSV, JSON natively supports complex data structures (like maps, arrays, and nested structures) and carries type information implicitly.


Single-Line vs. Multi-Line JSON

Spark's default JSON reader is optimized to read Single-Line JSON (where each line of the file is a complete, independent JSON record).

  • Single-Line JSON (Standard):
{"id":1, "name":"Alice", "skills":["PySpark","SQL"]}
{"id":2, "name":"Bob", "skills":["Java"]}
*Fast and parallelizable. Spark can split the file across multiple partitions easily.*
  • Multi-Line JSON (Formatted/Pretty-printed):
[
  {
    "id": 1,
    "name": "Alice",
    "skills": ["PySpark", "SQL"]
  }
]
*Requires loading the entire file into a single executor thread. Slower and cannot be easily split, requiring the `multiLine` option set to `"true"`.*

PySpark Code Example: Reading Nested JSON

Here is a complete script demonstrating how to ingest nested JSON files, enforce schemas, and extract values from nested arrays:

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType, ArrayType
from pyspark.sql.functions import col, explode

# 1. Setup Spark
spark = SparkSession.builder \
    .appName("JSON Ingestion") \
    .master("local[*]") \
    .getOrCreate()

# 2. Define schema with nested arrays
json_schema = StructType([
    StructField("id", IntegerType(), False),
    StructField("name", StringType(), True),
    StructField("skills", ArrayType(StringType()), True)
])

# 3. Read single-line JSON dataset
df = spark.read \
    .format("json") \
    .schema(json_schema) \
    .load("dataset.json")

df.show()
df.printSchema()

# 4. Explode array: Convert the array of skills into separate rows
exploded_df = df.select("name", explode("skills").alias("skill"))
exploded_df.show()

# 5. Write data back as compressed JSON
df.write \
    .format("json") \
    .mode("overwrite") \
    .option("compression", "bzip2") \
    .save("output_json_directory")
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.